package com.zychao.smex.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zychao.smex.db.service.BusinessService;
import com.zychao.smex.exception.CartNotFoundException;
import com.zychao.smex.po.Cart;

@SuppressWarnings("serial")
@WebServlet("/UpdateCartServlet")
public class UpdateCartServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		try {
			String bookId = request.getParameter("bookId");
			int count = Integer.parseInt(request.getParameter("count"));
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			BusinessService service = new BusinessService();
			service.updateCart(bookId, cart, count);
			request.getRequestDispatcher("/WEB-INF/jsp/listCart.jsp").forward(request, response);
		} catch(CartNotFoundException ex) {
			ex.printStackTrace();
			request.setAttribute("message", "对不起，修改失败");
			request.getRequestDispatcher("/message.jsp").forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
